﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp1
{
    public  class SqlHelper
    {
        public static string Constr { get; set; }
        public static DataTable ExecuteTable(String cmdStr)
        {
            using (SqlConnection connection=new SqlConnection(Constr))
            {
                connection.Open();
                SqlCommand sqlCommand = new SqlCommand(cmdStr,connection);
                SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand);
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet);
                return dataSet.Tables[0];
            }

        }
        public static int ExecuteNonQuery(string cmdStr)
        {
            //修改数据表的方法，返回受影响行数
            using (SqlConnection con = new SqlConnection(Constr))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(cmdStr, con);
                int rows = cmd.ExecuteNonQuery();
                if (rows <= 0)
                {
                    throw new Exception("Database operation failed");
                    //操作失败抛出异常
                }
                return rows;
            }
        }


    }
}
